import enums from '@/common/sysEnum.js';

const wxAPI = (opertType) => {
	let result;
	if(opertType === enums.enums.OPERTTYPE.WEIXIN_LOGIN) {
	   result= wxLogin()
	}
	else if (opertType === enums.enums.OPERTTYPE.WEIXIN_CHECK_SESSION) {
	   result = wxCheckSession()
	}
	else if (opertType === enums.enums.OPERTTYPE.WEIXIN_SETTING){
		result = wxSetting()
	}
	else if (opertType === enums.enums.OPERTTYPE.WEIXIN_AUTHORIZE){
		result = wxAuthorize(enums.enums.WEIXINSCOPE.SCOPE_USERINFO)
	}
	else if (opertType === enums.enums.OPERTTYPE.WEIXIN_USER_INFO) {
		result = wxUserInfo()
	}
	return result;
}


// 微信登录
const wxLogin = () => {
	return new Promise((res) => {
		 wx.login({
			success:(result) => {
            	res(result)
            },
            fail: (e) => {
            	res(e)
            }   
		})
	})
}

//微信检查Session方法
const wxCheckSession = () => {
	return new Promise((res) => {
		wx.checkSession({
			success:(result) => {
            	res(result)
            },
            fail: (e) => {
            	res(e)
            }   
		})
	})
}

//获取用户的当前设置
const wxSetting = () => {
	return new Promise((res) => {
		wx.getSetting({
			success:(result) => {
            	res(result)
            },
            fail: (e) => {
            	res(e)
            }   
		})
	})
}

//提前向用户发起授权请求。调用后会立刻弹窗询问用户是否同意授权小程序使用某项功能或获取用户的某些数据
const wxAuthorize = (scope) => {
	return new Promise((res) => {
		wx.authorize({
			scope: scope,
			success:(result) => {
            	res(result)
            },
            fail: (e) => {
            	res(e)
            }   
		})
	})
}

// 获取微信用户信息
const wxUserInfo = () => {
	return new Promise((resolve, reject) => {
		wx.getUserInfo({
			withCredentials: true,//是否带上登录态信息
			success:(result) => {
				resolve(result);   //获取成功
            },
            fail: (e) => {
            	reject(e);     //获取失败
            }   
		})
	})
}


export { 
	wxLogin,
	wxCheckSession,
	wxSetting,
	wxAuthorize,
	wxUserInfo,
	wxAPI
	 }